package com.lanxiao.cloud.modules.oauth.application;

import com.lanxiao.cloud.modules.oauth.application.assembler.RoleAssembler;
import com.lanxiao.cloud.modules.oauth.application.dto.RoleCmdAdd;
import com.lanxiao.cloud.modules.oauth.application.dto.RoleCmdUpdate;
import com.lanxiao.cloud.modules.oauth.domain.service.IRoleService;
import java.util.List;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ：qiaoliang
 * @date ：2021-09-16
 */
@Component
public class RoleCmdManager {

  private final IRoleService roleService;
  private final RoleAssembler roleAssembler;

  public RoleCmdManager(IRoleService roleService,
      RoleAssembler roleAssembler) {
    this.roleService = roleService;
    this.roleAssembler = roleAssembler;
  }

  @Transactional(rollbackFor = {RuntimeException.class, Exception.class, Error.class})
  public void  saveRole(RoleCmdAdd add) {
    roleService.saveRole(roleAssembler.toRole(add), add.getIds());
  }

  @Transactional(rollbackFor = {RuntimeException.class, Exception.class, Error.class})
  public void  updateRole(Long id, RoleCmdUpdate update) {
    roleService.updateRole(roleAssembler.toRole(id, update), update.getIds());
  }

  public void  deleteRole(Long id) {
    roleService.deleteRole(id);
  }

  @Transactional(rollbackFor = {RuntimeException.class, Exception.class, Error.class})
  public void updateUserAndRole(Long id, List<Long> roleIds){
    roleService.updateUserAndRole(id,roleIds);
  }

}
